package com.styytech.dadabao.ui.my.accountset;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.loopj.android.http.RequestParams;
import com.styytech.dadabao.DadaApplication;
import com.styytech.dadabao.R;
import com.styytech.dadabao.api.requestresult.BankCardResult;
import com.styytech.dadabao.api.response.AbstractResponseResult;
import com.styytech.dadabao.api.templates.HttpRequest;
import com.styytech.dadabao.base.BaseActivity;
import com.styytech.dadabao.constant.ConstantsCommon;
import com.styytech.dadabao.constant.ConstantsServerUrl;
import com.styytech.dadabao.exception.ArgumentsException;
import com.styytech.dadabao.exception.RequestException;
import com.styytech.dadabao.utils.CommonUtils;
import com.styytech.dadabao.utils.SpUser;
import com.styytech.dadabao.widge.dialog.CustomLoadingDialog;

/**
 * 添加银行卡绑定
 */
@ContentView(R.layout.activity_add_bankcard)
public class AddBankCardActivity extends BaseActivity {

    /*银行卡管理*/
    @ViewInject(R.id.llyt_add_bankcard)
    LinearLayout llyt_add_bankcard;
    /*持卡人*/
    @ViewInject(R.id.et_name)
    TextView et_name;
    /*银行名称*/
    @ViewInject(R.id.et_bank_name)
    EditText et_bank_name;
    /*开户行省份*/
    @ViewInject(R.id.et_bank_province)
    EditText et_bank_province;
    /*开户行城市*/
    @ViewInject(R.id.et_bank_city)
    EditText et_bank_city;
    /*开户行名称*/
    @ViewInject(R.id.et_subbranch_name)
    EditText et_subbranch_name;
    /*卡号*/
    @ViewInject(R.id.et_bankcard_number)
    EditText et_bankcard_number;
    /*下一步*/
    @ViewInject(R.id.bt_next)
    Button bt_next;

    /*银行卡详情*/
    @ViewInject(R.id.llyt_bankcard_successbind)
    LinearLayout llyt_bankcard_successbind;
    /*银行卡logo*/
    @ViewInject(R.id.iv_bankcard)
    ImageView iv_bankcard;
    /*银行名称*/
    @ViewInject(R.id.tv_bankcard_name)
    TextView tv_bankcard_name;
    /*银行卡号*/
    @ViewInject(R.id.tv_bankcard_id)
    TextView tv_bankcard_id;

    /*上下文*/
    private Context mContext;
    /*loading框*/
    CustomLoadingDialog mdlg;
    private String accountName;
    private String accountNo;
    private String bankName;
    private String bankProvince;
    private String bankCity;
    private String bankBranchName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        initView();
        initData();
    }

    /**
     * 初始化视图
     */
    private void initView() {
        DadaApplication.addActivity(this);
        setTopBar("添加银行卡", View.VISIBLE);


    }

    /**
     * 初始界面设置
     */
    private void initData() {
        accountName = SpUser.getUserInfo(mContext, ConstantsCommon.USER_REALNAME);
        et_name.setText(accountName);
    }

    @OnClick({R.id.bt_next})
    public void click(View view) {
        switch (view.getId()) {
            case R.id.bt_next:// 下一步
                if (bt_next.getText().toString().trim().equals("完 成")) {
                    finish();
                } else {
                    submit();
                }
                break;
        }
    }

    private void submit() {
        /*获取用户输入的账号，密码*/

        bankName = et_bank_name.getText().toString().trim();
        bankProvince = et_bank_province.getText().toString().trim();
        bankCity = et_bank_city.getText().toString().trim();
        bankBranchName = et_subbranch_name.getText().toString().trim();
        accountNo = et_bankcard_number.getText().toString().trim();

        /*条件过滤*/
        if (bankName.isEmpty()) {
            toast("银行名称不能为空");
            return;
        } else if (bankProvince.isEmpty()) {
            toast("开户行省份不能为空");
            return;
        } else if (bankCity.isEmpty()) {
            toast("开户行城市不能为空");
            return;
        } else if (bankBranchName.isEmpty()) {
            toast("开户行名称不能为空");
            return;
        } else if (accountNo.isEmpty()) {
            toast("银行卡卡号不能为空");
            return;
        }
        mdlg = CommonUtils.getCustomLoadingDialog(context, mdlg, "绑定银行卡", true);
        mdlg.show();
        /*绑定银行卡请求参数*/
        RequestParams params = new RequestParams();
        params.put("accountNo", accountNo);
        params.put("accountName", accountName);
        params.put("accountType", 1);
        params.put("bankName", bankName);
        params.put("bankProvince", bankProvince);
        params.put("bankCity", bankCity);
        params.put("bankBranchName", bankBranchName);
        /*提交实名认证请求*/
        submitRequest(params);
    }


    /**
     * 提交审核请求
     *
     * @param params 请求参数
     */
    private void submitRequest(RequestParams params) {
        String url = ConstantsServerUrl.getSaveUserCardUrl();// 添加银行卡绑定接口
        //获取验证码
        BankCardResult submitResult = new BankCardResult(responseResult);
        try {
            new HttpRequest(this).doPost(submitResult, url, params);
        } catch (ArgumentsException e) {
            Log.i("INFO", "1e:" + e);
            e.printStackTrace();
        } catch (RequestException e) {
            Log.i("INFO", "2e:" + e);
            e.printStackTrace();
        }
    }

    /**
     * 提交请求结果处理
     */
    AbstractResponseResult responseResult = new AbstractResponseResult() {

        @Override
        public void executeSuccess(Object object) {
            SpUser.setBankCardVStatus(mContext, 1);
            setTopBar("银行卡详情", View.VISIBLE);
            llyt_add_bankcard.setVisibility(View.GONE);
            llyt_bankcard_successbind.setVisibility(View.VISIBLE);
            tv_bankcard_name.setText(object.toString());
            String subAccountNo1 = accountNo.substring(3, accountNo.length() - 4);
            String subAccountNo2 = accountNo.substring(accountNo.length() - 4, accountNo.length());
            String subAccountNo = "";
            for (int i = 0; i < subAccountNo1.length(); i++) {
                subAccountNo += "*";
            }
            accountNo = accountNo.replace(subAccountNo1, subAccountNo);
            tv_bankcard_id.setText(accountNo);
            bt_next.setText("完 成");
            CommonUtils.closeDialog(mdlg);
        }

        @Override
        public void executeError(String message, int errorCode) {
            CommonUtils.showToastMsg(context, "" + message);
            CommonUtils.closeDialog(mdlg);
        }

        @Override
        public void HttpError(String message, int errorCode) {
            CommonUtils.showToastMsg(context, "" + message);
            CommonUtils.closeDialog(mdlg);
        }
    };

    /**
     * 启动界面
     *
     * @param cls 需要启动的界面类
     */
    private void startNextActivity(Class<?> cls) {
        Intent intent = new Intent(mContext, cls);
        startActivity(intent);
    }

    /**
     * 提示消息
     *
     * @param str 需要提示的消息
     */
    private void toast(String str) {
        Toast.makeText(mContext, str, Toast.LENGTH_SHORT).show();
    }

}

